//
//  JFBLoadingAnimatioin.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/5/11.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBLoadingAnimation.h"

static NSString *kAnimationNameKey = @"animation_name";
//static NSString *kAnimationPFNameKey = @"animation_pf_name";
static NSString *kScrapDriveUpAnimationName = @"scrap_drive_up_animation";
static NSString *kScrapDriveDownAnimationName = @"scrap_drive_down_animation";
static NSString *kScrapPFUpAnimationName = @"scrap_pf_up_animation";
static NSString *kScrapPFDownAnimationName = @"scrap_pf_down_animation";
//static NSString *kBucketDriveUpAnimationName = @"bucket_drive_up_animation";
//static NSString *kBucketDriveDownAnimationName = @"bucket_drive_down_animation";

//static const CGFloat kScrapDriveUpAnimationHeight = 200;
static const CGFloat kScrapYOffsetFromBase = 7;

@interface JFBLoadingAnimation()
@property (nonatomic, strong) CALayer *scrapLayer;
@property (nonatomic, strong) CALayer *scrapLayer_pf;
@property (nonatomic, strong) UIView *view;
@property (nonatomic, assign) CGFloat baseviewYOrigin;
@property (nonatomic, assign) CFTimeInterval duration;
@end

@implementation JFBLoadingAnimation

-(instancetype)init:(UIView*)view {
    self = [super init];
    if( self ) {
        self.duration = 0.6;
        self.view = view;
        [self.view setUserInteractionEnabled: NO];
        [self initView];
    }
    return self;
}

- (CGRect)CGRectIntegralCenteredInRect:(CGRect)innerRect withRect:(CGRect)outerRect
{
    CGFloat originX = floorf((outerRect.size.width - innerRect.size.width) * 0.5f);
    CGFloat originY = floorf((outerRect.size.height - innerRect.size.height) * 0.5f);
    CGRect bounds = CGRectMake(originX, originY, innerRect.size.width, innerRect.size.height);
    return bounds;
}

- (void)initView {
    CGRect rect_pf = [self CGRectIntegralCenteredInRect:CGRectMake(0, 0, 42, 20) withRect:self.view.frame];
    self.baseviewYOrigin = rect_pf.origin.y;
    
    UIImage *img_pf = [UIImage imageNamed:@"piu-pf"];
    rect_pf.origin.y = self.baseviewYOrigin - kScrapYOffsetFromBase + 10;
    rect_pf.origin.x = rect_pf.origin.x + 10;
    
    self.scrapLayer_pf = [CALayer layer];
    self.scrapLayer_pf.frame = rect_pf;
    self.scrapLayer_pf.bounds = rect_pf;
    [self.scrapLayer_pf setContents:(id)img_pf.CGImage];
    [self.view.layer addSublayer:self.scrapLayer_pf];
    
    // set base view y origin
    CGRect rect = [self CGRectIntegralCenteredInRect:CGRectMake(0, 0, 45, 58) withRect:self.view.frame];
    self.baseviewYOrigin = rect.origin.y;
    
    UIImage *img = [UIImage imageNamed:@"piu"];
    rect.origin.y = self.baseviewYOrigin - kScrapYOffsetFromBase;
    
    self.scrapLayer = [CALayer layer];
    self.scrapLayer.frame = rect;
    self.scrapLayer.bounds = rect;
    [self.scrapLayer setContents:(id)img.CGImage];
    [self.view.layer addSublayer:self.scrapLayer];
    
    [self scrapPFUpAnimation];
    [self scrapDriveUpAnimation];
    
    
//    [self scrapDriveUpAnimation];

}

- (void)scrapPFUpAnimation
{
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.fromValue = [NSValue valueWithCGPoint:self.scrapLayer_pf.position];
    CGPoint point = CGPointMake(CGRectGetMidX(self.scrapLayer_pf.frame) - 10, CGRectGetMidY(self.scrapLayer_pf.frame) - 10);
    moveAnimation.toValue =  [NSValue valueWithCGPoint:point];
    //    [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(self.scrapLayer.frame), CGRectGetMidY(self.scrapLayer.frame) - kScrapDriveUpAnimationHeight)];
    moveAnimation.removedOnCompletion = NO;
    moveAnimation.fillMode = kCAFillModeForwards;
    moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
//    
//    CAKeyframeAnimation* keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
//    CATransform3D forward, back, back2, forward3, back3, forward4, back4, up2, down2, up3, down3, up4, down4;
//    ///设置动画
////    up = CATransform3DMakeRotation(M_PI * 0.1, 0, 0, 1);
//    forward = CATransform3DMakeScale(1.3, 1.3, 1);
////    down = CATransform3DMakeRotation(0.0, 0, 0, 1);
//    back = CATransform3DMakeScale(0.7, 0.7, 1);
//    
//    up2 = CATransform3DMakeRotation(M_PI * 0.05, 0, 0, 1);
////    forward2 = CATransform3DMakeScale(1.0, 1.0, 1);
//    down2 = CATransform3DMakeRotation(0.0, 0, 0, 1);
//    back2 = CATransform3DMakeScale(0.85, 0.85, 1);
//    
//    up3 = CATransform3DMakeRotation(M_PI * 0.05, 0, 0, 1);
//    forward3 = CATransform3DMakeScale(1.1, 1.1, 1);
//    down3 = CATransform3DMakeRotation(0.0, 0, 0, 1);
//    back3 = CATransform3DMakeScale(0.9, 0.9, 1);
//    
//    up4 = CATransform3DMakeRotation(M_PI * 0.05, 0, 0, 1);
//    forward4 = CATransform3DMakeScale(1.05, 1.05, 1);
//    down4 = CATransform3DMakeRotation(0.0, 0, 0, 1);
//    back4 = CATransform3DMakeScale(0.95, 0.95, 1);
//    
//    
//    [keyFrameAnimation setValues:[NSArray arrayWithObjects:
//                                  [NSValue valueWithCATransform3D:CATransform3DIdentity],
//                                  [NSValue valueWithCATransform3D:forward],
//                                  [NSValue valueWithCATransform3D:back],
//                       [NSValue valueWithCATransform3D:CATransform3DIdentity],nil]];
    
    NSArray* scaleKeyFrameValues = @[
                                @(1.2),
                                @(1.0),
                                @(0.8),
                                @(1.0),
                                @(1.2)
                                ];
    
    CAKeyframeAnimation* scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    [scaleAnimation setValues:scaleKeyFrameValues];
//    [scaleAnimation setValueFunction:[CAValueFunction functionWithName: kCAValueFunctionRotateZ]];
    scaleAnimation.removedOnCompletion = NO;
    scaleAnimation.fillMode = kCAFillModeForwards;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.delegate = self;
    [animGroup setValue:kScrapPFUpAnimationName forKey:kAnimationNameKey];
    //    animGroup.animations = @[moveAnimation, rotateAnimation];
    animGroup.animations = @[moveAnimation, scaleAnimation];
    animGroup.duration = self.duration;
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeForwards;
    [self.scrapLayer_pf addAnimation:animGroup forKey:nil];
}

- (void)scrapPFDownAnimation
{
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.delegate = self;
    [moveAnimation setValue:kScrapPFDownAnimationName forKey:kAnimationNameKey];
    //    CGPoint point = CGPointMake(self.scrapLayer.position.x - 30, self.scrapLayer.position.y - 30);
    CGPoint point = CGPointMake(self.scrapLayer_pf.position.x, self.scrapLayer_pf.position.y);
    moveAnimation.toValue = [NSValue valueWithCGPoint:point];
    moveAnimation.duration = self.duration;
    moveAnimation.removedOnCompletion = NO;
    moveAnimation.fillMode = kCAFillModeForwards;
    moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [self.scrapLayer_pf addAnimation:moveAnimation forKey:nil];
}

- (void)scrapDriveUpAnimation
{
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.fromValue = [NSValue valueWithCGPoint:self.scrapLayer.position];
    CGPoint point = CGPointMake(CGRectGetMidX(self.scrapLayer.frame) - 10, CGRectGetMidY(self.scrapLayer.frame) - 10);
    moveAnimation.toValue =  [NSValue valueWithCGPoint:point];
//    [NSValue valueWithCGPoint:CGPointMake(CGRectGetMidX(self.scrapLayer.frame), CGRectGetMidY(self.scrapLayer.frame) - kScrapDriveUpAnimationHeight)];
    moveAnimation.removedOnCompletion = NO;
    moveAnimation.fillMode = kCAFillModeForwards;
    moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
//    NSArray* keyFrameValues = @[
//                                @(0.0),
//                                @(M_PI),
//                                @(M_PI*1.5),
//                                @(M_PI*2.0)
//                                ];
//    CAKeyframeAnimation* rotateAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
//    [rotateAnimation setValues:keyFrameValues];
//    [rotateAnimation setValueFunction:[CAValueFunction functionWithName: kCAValueFunctionRotateZ]];
//    rotateAnimation.removedOnCompletion = NO;
//    rotateAnimation.fillMode = kCAFillModeForwards;
//    rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    animGroup.delegate = self;
    [animGroup setValue:kScrapDriveUpAnimationName forKey:kAnimationNameKey];
//    animGroup.animations = @[moveAnimation, rotateAnimation];
    animGroup.animations = @[moveAnimation];
    animGroup.duration = self.duration;
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeForwards;
    [self.scrapLayer addAnimation:animGroup forKey:nil];
}

- (void)scrapDriveDownAnimation
{
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.delegate = self;
    [moveAnimation setValue:kScrapDriveDownAnimationName forKey:kAnimationNameKey];
//    CGPoint point = CGPointMake(self.scrapLayer.position.x - 30, self.scrapLayer.position.y - 30);
    CGPoint point = CGPointMake(self.scrapLayer.position.x, self.scrapLayer.position.y);
    moveAnimation.toValue = [NSValue valueWithCGPoint:point];
    moveAnimation.duration = self.duration;
    moveAnimation.removedOnCompletion = NO;
    moveAnimation.fillMode = kCAFillModeForwards;
    moveAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    [self.scrapLayer addAnimation:moveAnimation forKey:nil];
}

#pragma mark - Animation Delegate methods

- (void)animationDidStart:(CAAnimation *)anim
{
//    NSString *animationName = [anim valueForKey:kAnimationNameKey];
//    if ([animationName isEqualToString:kScrapDriveDownAnimationName]) {
//        [self bucketDriveUpAnimation];
//        
//    }
//    else if ([animationName isEqualToString:kBucketDriveUpAnimationName]) {
//        self.bucketContainerLayer.hidden = NO;
//        [self.bucket performSelector:@selector(openBucket) withObject:nil afterDelay:self.duration * 0.3];
//    }
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if (flag) {
        NSString *animationName = [anim valueForKey:kAnimationNameKey];
        if ([animationName isEqualToString:kScrapDriveUpAnimationName]) {
            [self performSelector:@selector(scrapDriveDownAnimation) withObject:nil afterDelay:self.duration * 0.1];
            
        } else if ([animationName isEqualToString:kScrapDriveDownAnimationName]) {
            [self performSelector:@selector(scrapDriveUpAnimation) withObject:nil afterDelay:self.duration * 0.1];
            
        } else if([animationName isEqualToString:kScrapDriveUpAnimationName]) {
            [self performSelector:@selector(scrapDriveDownAnimation) withObject:nil afterDelay:self.duration * 0.1];
        } else if ([animationName isEqualToString:kScrapPFDownAnimationName]) {
            [self performSelector:@selector(scrapPFUpAnimation) withObject:nil afterDelay:self.duration * 0.1];
            
        } else if([animationName isEqualToString:kScrapPFUpAnimationName]) {
            [self performSelector:@selector(scrapPFDownAnimation) withObject:nil afterDelay:self.duration * 0.1];
        }
    }
}

-(void)stop{
    [self.scrapLayer removeFromSuperlayer];
    [self.scrapLayer_pf removeFromSuperlayer];
    [self.view setUserInteractionEnabled: YES];
}
@end
